import 'layui-src';
layui.config({
    dir: '/dist/'
})
import 'layui-src/src/css/layui.css';
import 'layui-src/src/css/modules/laydate/default/laydate.css';
import 'layui-src/src/css/modules/layer/default/layer.css';
import 'layui-src/src/css/modules/code.css';
import 'layui-src/src/css/modules/layer/default/icon.png';

import $ from "../jquery-3.4.1.min.js";
import "../../css/update.css"
import ajax from "../../utils/ajsx.js";
function renderForm() {
    layui.use('form', function () {
        var form = layui.form;
        form.render();
    });
}

layui.use('form', function () {
    var form = layui.form;

    //监听提交
    form.on('submit(formDemo)', function (data) {
        layer.msg(JSON.stringify(data.field));
        return false;
    });
});

$("#return").click(function () {
    history.go(-1);
})


$(async function () {
    let id = location.search.substr(4);
    const {code,data:[{_id,name,ClassificationId,communityId,contentData,top}]} =  await ajax({ url: "Announcement/updateOne", type: "post", data: {id} });
    // console.log(name)
    $('[name=title]').val(name);
    $('[name=announcement-data]').val(contentData);
    $('#clk').attr("data-val",_id);
    if(top){
        $('[name=top]').attr("checked",true);
    }
    const res = await ajax({ url: "Classification/getData", type: "get", data: {} });
      const ops = res.data.map(item => {
        if (item._id == ClassificationId) {
          return `
          <option value="${item._id}" selected="selected">${item.name}</option>
        `
        } else {
          return `
          <option value="${item._id}">${item.name}</option>
        `
        }
      }).join("");

      $('[name=announcement]').html(ops);
      
    const resArr = await ajax({ url: "Community/getList", type: "post", data: {} });
    // console.log()
   
    if (resArr.code) {
    

      const Communityops = resArr.data.map(item => {
        if (item._id == communityId) {
          return `
          <option value="${item._id}" selected="selected">${item.name}</option>
        `
        } else {
          return `
          <option value="${item._id}">${item.name}</option>
        `
        }
      }).join("");
      $('[name=associated]').html(Communityops);
      renderForm();
    }

    // console.log(result);
})
function getCurrentDate() {
    const date = new Date();
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    var h = date.getHours();
    var min = date.getMinutes();
    var s = date.getSeconds();
    var str = y + '年' + (m < 10 ? ('0' + m) : m) + '月' + (d < 10 ? ('0' + d) : d) + '日  ' + (h < 10 ? ('0' + h) : h) + ':' + (min < 10 ? ('0' + min) : min) + ':' + (s < 10 ? ('0' + s) : s);
    return str;
  }
$("#clk").click(async function(e){
    e.preventDefault();
    const id = $(e.target).data("val");
    const name = $('[name=title]').val();
    const ClassificationId = $('[name=announcement]').val();
    const communityId = $('[name=associated]').val();
    const userId = localStorage.username;
    const updateTime = getCurrentDate();
    const top = $('[name=top]').is(":checked") ? 1 : 0;
    const contentData =  $("[name=announcement-data]").val();
    // console.log();

    const {code,message} = await ajax({ url: "Announcement/updateData", type: "post", data: {id,name,ClassificationId,communityId,userId,updateTime,top,contentData} });
    if(code){
        alert(message);
        history.go(-1);
    }
})